<?php

/* 
 * 商家控制器
 * @author wyb
 */
namespace Qwadmin\Controller;
class CustomerController extends  ComController {
    
    private $_model = null;
    
    public function _initialize() {
        parent::_initialize();
        $this->_model = D('Customer');
    }
    
    /**
     * 商家列表
     */
    public function Index() {
        $where = array();
        $page = I('get.p') ? I('get.p') : 1;
        $data = $this->_model->where( $where )->page( $page , $this->_row )->order( 'id DESC' )->select();
        $total = $this->_model->where( $where )->count();
        $this->__assignPage( $total );
        $this->assign( 'lists' , $data );
        $this->_ApiPlagAuthList();
        $this->display();
    }
    
    /**
     * 开通商家
     */
    public function Insert() {      
        if( IS_POST ) {
            $data = I('post.poster') ? I('post.poster') : $this->error( '发生未知错误！');
            $data['api_role'] = $data['api_role'] ? serialize($data['api_role']) : '';
            $data['auth_time'] = $data['auth_time'] ? strtotime( $data['auth_time'] ) : 0;
            $data['password'] = password($data['password']);
            if(M('customer')->where(array('username'=>$data['username']))->find()){
                 $this->error( '添加失败,用户名重复' );
            }
            if( $this->_model->create( $data ) === false ) {
                $this->error( $this->_model->getError() );
            }
            if( $this->_model->add() === false ) {
                $this->error( '添加失败' );
            }
            $this->success( '信息添加成功！' , U('Customer/Index'));
        } else {
             $this->assign('company',M('company')->select());
            $this->_ApiPlagAuthList();
            $this->display();
        }
    }
    
    /**
     * 商家修改
     */
    public function Update() {
        $id = I('get.id') ? I('get.id') : $this->error( '发生了一个错误！' );
        if( !$data = $this->_model->find( $id ) ) $this->error( '商家不存在！' );
        if( IS_POST ) {
            $data = I('post.poster') ? I('post.poster') : $this->error( '发生未知错误！');
            $data['api_role'] = $data['api_role'] ? serialize($data['api_role']) : '';
            $data['auth_time'] = $data['auth_time'] ? strtotime( $data['auth_time'] ) : 0;
            if($data['password']){
                 $data['password'] =password($data['password']);
            }else{
                unset($data['password']);
            }
           
            if(M('customer')->where(array('username'=>$data['username'],'id'=>array('neq',$data['id'])))->find()){
                 $this->error( '添加失败,用户名重复' );
            }
        
            if( $this->_model->create( $data ) === false ) {
                $this->error( $this->_model->getError() );
            }
            if( $this->_model->save() === false ) {
                $this->error( '修改失败' );
            }
            $this->success( '信息修改成功！' , U( 'Customer/Index' ) );
        } else {
            $data['api_role'] = $data['api_role'] ? unserialize($data['api_role']) : '';
            $this->_ApiPlagAuthList( $data['api_role'] );
            $this->assign( 'data' , $data );
             $this->assign('company',M('company')->select());
            $this->display();
        }
    }
    
    /**
     * 商家删除
     */
    public function Delete() {
        $id = I('get.id') ? I('get.id') : $this->error( '发生了一个错误！' );
        if( !$data = $this->_model->find( $id ) ) $this->error( '商家不存在！' );
        if( $this->_model->where(array('id' => $id))->delete() === flase ) {
            $this->error( '删除失败' );
        } else {
            $this->success( '删除成功' );
        }
    }
    
    /**
     * 获得API权限列表
     */
    private function _ApiPlagAuthList( $result = '' ) {
        $data = M('api_module')->field('id,title,module')->select();
		$result_key_data = array_keys( $result );
        foreach( $data as $key => $value ) {
            if( $result ) {
                $data[$key]['checked'] = 0;
                foreach( $result as $k => $v ) {
                    if( strpos( $k , $value['module'] ) !== false ) {
                        $data[$key]['checked'] = 1;
                    }
                }
            }
            $method_data = M('api_method')->where(array('module_id' => $value['id']))->field('id,title,method,fields,mode_type')->select();
            foreach( $method_data as $k => $v ) {
				$method_data[$k]['checked'] = 0;
				$auth_key = $value['module'] . '.' . $v['method'];
				if( in_array( $auth_key , $result_key_data ) ) {
					$method_data[$k]['checked'] = 1;
				}
                $method_data[$k]['fields'] = unserialize( $v['fields'] );
                if( $method_data[$k]['checked'] == 1 ) {
                    $key_data = $result[$auth_key]['fields'] ? array_keys( $result[$auth_key]['fields'] ) : array_column($method_data[$k]['fields'], 0 );
                    foreach( $method_data[$k]['fields'] as $m => $n ) {
                        $method_data[$k]['fields'][$m]['checked'] = 0;
                        if( in_array( $n[0] , $key_data ) ) {
                            $method_data[$k]['fields'][$m]['checked'] = 1;
                        }
                    }
                }
            }
            $data[$key]['list'] = $method_data;
        }
        $this->assign( 'auth_list' , $data );

        //支付方式配置
        $this->assign( 'pay_data' , M('payment_config')->where(array('status' => 1))->select());
    }
    
    public function saveStatus() {
        if( isset( $_GET['id']) && intval( $_GET['id'] ) > 0 && intval( $_GET['status'] ) >= 0 ) {
            M('customer')->where(array('id' => $_GET['id']))->save(array('status' => $_GET['status']));
        }
    }
}

